<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\QuestionGroup;
use app\common\model\User;
use app\common\model\UserClockIn;
use PDOException as GlobalPDOException;
use think\Db;
use think\db\exception\BindParamException;
use think\exception\PDOException;
use think\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\db\exception\DataNotFoundException;
use think\exception\HttpResponseException;
use think\Exception;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['agreement'];
    protected $noNeedRight = ['*'];


    /**
     * 首页
     */
    public function index()
    {
        $user_id = $this->auth->id;
        # 查询自己添加刷题组卷
        $question_cate_id = $this->request->param('question_cate_id', 0); # 传一级分类id
        $question_group_ids = Db::name('user_question')->where('user_id', $user_id)->where('question_cate_id', $question_cate_id)->column('question_group_id');
        $list = (new QuestionGroup())->getList(['question_group_ids' => $question_group_ids], 0, $user_id);
        $this->success('请求成功', [
            'question_group_list' => empty($question_group_ids) ? [] : $list,
            'clock_in_study' => UserClockIn::getStudyClockInCount($user_id), # 学习
            'clock_in_essay' => UserClockIn::getEssayClockInCount($user_id), # 打卡
        ]);
    }


    /**
     * 开关控制
     */
    public function switch(){
        $this->success('请求成功', [
            'dynamic' => 1,  //1隐藏 0展示
            'video' => 1, //1隐藏 0展示
        ]);
    }



    /**
     * 首页组卷模块
     */
    public function questionGroupList()
    {
        $user_id = $this->auth->id;
        $question_cate_id2 = $this->request->param('question_cate_id2', 0); # 二级分类
        $question_group_ids = Db::name('user_question')->where('user_id', $user_id)->where('question_cate_id2', $question_cate_id2)->column('question_group_id');
        if (empty($question_group_ids)) {
            $this->success('请求成功', []);
        }
        $list = (new QuestionGroup())->getList(['question_group_ids' => $question_group_ids], 0, $user_id);
        $this->success('请求成功', $list);
    }


    /**
     * 客服二维码
     */
    public function customer()
    { 
        $this->success('请求成功', [
            'customer_hotline' => config('site.customer_hotline'),
            'customer_code' => config('site.customer_code'),
        ]);
    }


    /**
     * 关于我们
     * @return void
     */
    public function aboutUc(){
        $this->success('请求成功', [
            'logo' => config('site.logo'),
            'about' => config('site.about'),
            'name' => config('site.name'),
        ]);
    }

    /**
     * 协议
     * @return void
     */
    public function agreement(){
        $this->success('请求成功', [
            'user_agreement' => config('site.user_agreement'),
            'privacy_policy' => config('site.privacy_policy'),
        ]);
    }
}
